﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BookMS
{
    public partial class user2 : Form
    {
        Model.book m_binfo = new Model.book();
        Model.lend m_lend = new Model.lend();
        BLL.Base table = new BLL.Base();
        public user2()
        {
            InitializeComponent();           
        }
     
        private void user2_Load(object sender, EventArgs e)
        {
            Table();
        }
        //从数据库读取数据显示在表格控件中
        public void Table()
        {
            dataGridView1.DataSource = table.table();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            m_binfo.id = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
            m_lend.bid = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();//获取书号
            m_lend.uid = Data.UID;
            int number = int.Parse(dataGridView1.SelectedRows[0].Cells[4].Value.ToString());//库存           
            if(number<1)
            {
                MessageBox.Show("库存不足，请联系管理员购入！");
            }
            else
            {
                //string sql = $"insert into lend (uid,bid,jieshuriqi)values('{Data.UID}','{id}',now());update book set number=number-1 where id='{id}'";
                if(table.lend(m_lend, m_binfo) > 1)
                {
                    MessageBox.Show($"用户{Data.UName}借出了图书{m_lend.bid}！");
                    Table();
                }

                //dataBase.DataBaseClose();
            }
        }
    }
}
